package com.anolesoft.Planexecution.fcfhprint.service.mapper;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.Planexecution.fcfhprint.dto.PrintfinfoDto;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface PrintfinfoMapper {

    /**
     * 根据条件查询所有数据
     */
    @SelectProvider(type= PrintfinfoMapper.createSQL.class, method = "query")
    List<PrintfinfoDto> listall(PrintfinfoDto dto);
    public static class createSQL {
        public static String query(PrintfinfoDto dto){
            return new SQL(){
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" *\n" +
                            "  from (select *\n" +
                            "          from (select b1.*, b2.jianyanyuan, rownum num1\n" +
                            "                  from (SELECT t.objuid,\n" +
                            "                               t.fabrecordid,\n" +
                            "                               t.jixing,\n" +
                            "                               t.tuhao,\n" +
                            "                               t.name,\n" +
                            "                               t.finfo,\n" +
                            "                               t.pgh,\n" +
                            "                               t.dw,\n" +
                            "                               t.code,\n" +
                            "                               to_char(t.f_time, 'yyyy-MM-dd') f_time,\n" +
                            "                               s.name code_user,\n" +
                            "                               to_char(t.d_time, 'yyyy-MM-dd') d_time,\n" +
                            "                               t.c_type,\n" +
                            "                               t.factory_id,\n" +
                            "                               t.db_sl,\n" +
                            "                               to_char(t.p_time, 'yyyy-MM-dd') p_time,\n" +
                            "                               t.p_num,\n" +
                            "                               y.name p_user,\n" +
                            "                               e.name operator,\n" +
                            "                               f.pc || f.jc pjc\n" +
                            "                          from MES_SS_PRODUCT_PRINTFINFO t\n" +
                            "                          left join (select objuid, name from  stf_employee) s\n" +
                            "                            on t.code_user = s.objuid\n" +
                            "                          left join (select objuid, barcode, planuid from mes_bzz_order) o\n" +
                            "                            on t.fabrecordid = o.barcode\n" +
                            "                          left join (select objuid, pc, jc from mes_bzz_fcfhjh) f\n" +
                            "                            on f.objuid = o.planuid\n" +
                            "                          left join (select objuid, barcodeobjuid, operator, operationnumber from mes_bzz_process)  p\n" +
                            "                            on p.barcodeobjuid = o.objuid\n" +
                            "                          left join (select objuid, name from  stf_employee) e\n" +
                            "                            on p.operator = e.objuid\n" +
                            "                          left join (select objuid, name from  stf_employee) y\n" +
                            "                            on t.p_user = y.objuid\n" +
                            "                         where p.operationnumber = '10'\n" +
                            "                            or t.fabrecordid is null) b1\n" +
                            "                  left join (SELECT t.objuid, e.name jianyanyuan\n" +
                            "                              from (select objuid, fabrecordid from MES_SS_PRODUCT_PRINTFINFO) t\n" +
                            "                              left join (select objuid, barcode, planuid from mes_bzz_order) o\n" +
                            "                                on t.fabrecordid = o.barcode\n" +
                            "                              left join (select objuid, barcodeobjuid, jianyanyuan, operationnumber from mes_bzz_process) p\n" +
                            "                                on p.barcodeobjuid = o.objuid\n" +
                            "                              left join (select objuid, name from  stf_employee) e\n" +
                            "                                on p.jianyanyuan = e.objuid\n" +
                            "                             where p.operationnumber = '60'\n" +
                            "                                or t.fabrecordid is null) b2" +
                            "                  on b1.objuid = b2.objuid");
                    sql.append(" where  b1.factory_id = #{factoryId}");
                    if(dto.getFabrecordid() != null){
                        String fabrecordid = dto.getFabrecordid().trim();
                        sql.append(" and b1.fabrecordid = #{fabrecordid} ");
                    }
                    if(dto.getCType() != null){
                        String cType = dto.getCType();
                        sql.append(" and b1.c_type = #{cType}");
                    }
                    sql.append(")");
//                    sql.append(" order by b1.fabrecordid");
                    if(dto.getPageBug() != null){
                        Integer pageBug = dto.getPageBug();
                        sql.append(" where num1<=#{pageBug})");
                    }
                    if(dto.getPageSmall() != null){
                        Integer pageSmall = dto.getPageSmall();
                        sql.append(" where num1>#{pageSmall}");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }


    /**
     * 查询总条数
     */
    @SelectProvider(type = PrintfinfoMapper.createSQL1.class, method = "query")
    int counttiao(PrintfinfoDto dto);

    public static class createSQL1 {
        public static String query(PrintfinfoDto dto) {
            return new SQL() {
                {
                    StringBuilder sql = new StringBuilder();
                    sql.append(" count(1) from" +
                            "(select b1.* from" +
                            "  (SELECT t.objuid," +
                            "       t.fabrecordid," +
                            "       t.c_type," +
                            "       t.factory_id" +
                            "  from MES_SS_PRODUCT_PRINTFINFO t" +
                            "  left join mes_bzz_order o" +
                            "    on t.fabrecordid = o.barcode" +
                            "  left join mes_bzz_process p" +
                            "    on p.barcodeobjuid = o.objuid" +
                            "  where  p.operationnumber = '10'" +
                            "  or t.fabrecordid is null) b1" +
                            "  left join " +
                            "  (SELECT t.objuid" +
                            "  from MES_SS_PRODUCT_PRINTFINFO t" +
                            "  left join mes_bzz_order o" +
                            "    on t.fabrecordid = o.barcode" +
                            "  left join mes_bzz_process p" +
                            "    on p.barcodeobjuid = o.objuid" +
                            "  where  p.operationnumber = '60'" +
                            "  or t.fabrecordid is null) b2" +
                            "  on b1.objuid = b2.objuid)");
                    sql.append(" where factory_id = #{factoryId}");
                    if (dto.getFabrecordid() != null) {
                        String fabrecordid = dto.getFabrecordid().trim();
                        sql.append(" and fabrecordid = #{fabrecordid} ");
                    }
                    if (dto.getCType() != null) {
                        String cType = dto.getCType();
                        sql.append(" and c_type = #{cType}");
                    }
                    SELECT(sql.toString());
                }
            }.toString();
        }
    }


    /**
     * 打印之后传回打印主键,打印次数加1.
     */
    @Update("update mes_ss_product_printfinfo t set t.p_time = sysdate,t.p_num = t.p_num+1,t.p_user = #{puser} where t.objuid = #{objuid}")
    int updateprint(@Param("puser")String puser, @Param("objuid")String objuid);

    /**
     * 打印之后传回打印主键,打印次数加1.
     */
    @Update("update mes_ss_product_printfinfo t set t.p_time = sysdate,t.p_num = 1,t.p_user = #{puser} where t.objuid = #{objuid}")
    int updateprintzero(@Param("puser")String puser, @Param("objuid")String objuid);

    /**
     * 查询打印表中打印数量是否为0
     */
    @Select("select nvl(t.p_num,0) from mes_ss_product_printfinfo t where t.objuid = #{objuid}")
    Integer countprint(@Param("objuid")String objuid);
}
